package com.thb.business.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.thb.business.dao.IColumnDao;
import com.thb.business.model.Column;

@Repository
public class ColumnDaoImpl implements IColumnDao {

	@Autowired
	private JdbcTemplate jTemplate;

	@Override
	public List<Column> findAll(Column para) {
		String sql = "select * from t_column where type= ? order by id limit ?,? ";
		Object[] arg = new Object[] {para.getType(),(para.getPage()-1)*para.getPageSize(),para.getPageSize()};
		
		return jTemplate.query(sql, arg,new RowMapper<Column>() {
			public Column mapRow(ResultSet rs, int no) throws SQLException {

				return jsonMapper(rs, no);
			}
		});
	}
	
	@Override
	public int countAll(Column para) {
		String sql = "select count(*) from t_column where type= ? ";
		Object[] arg = new Object[] {para.getType()};
		
		return jTemplate.queryForInt(sql, arg);
	}

	private Column jsonMapper(ResultSet rs, int no) throws SQLException {

		Column vo = new Column();
		vo.setId(rs.getInt("id"));
		vo.setName(rs.getString("name"));
		vo.setStatus(rs.getInt("status"));
		
		return vo;
	}

	

}
